extern crate rusqlite;

use rusqlite::NO_PARAMS;
use rusqlite::{Connection, Result};
use std::collections::HashMap;
use super::parts;
#[derive(Debug)]
struct Todo {
    id:i32,
    text: String,
    completed: bool,
}
pub fn main(){
    println!("runing");
   let r=main_do();
   match r {
        Ok(i) => {
            println!("ok");
        },
        Err(v) => {
            println!("{}",v);
        }
   };
}
// fn del_id(id:&str)-> rusqlite::Result<()> {
//     println!("{}",id);
//     let constr=String::from("c:\\ncs\\ncs_cs\\cs\\data.db");
//     let conn = rusqlite::Connection::open(constr)?;
//     del_one(conn,String::from(id));
//     Ok(())
// }
// fn get_one(conn:rusqlite::Connection,id1:String)->(String,i32) {
//     println!("=========");
//     let j1=minute_j1(id1.clone());
//     println!("{},{}",id1,j1);
//     let mut stmt = conn.prepare(
//         "select * from result where SampleId>=? and SampleId<=?",
//     ).expect("error prepare");
//     let cats = stmt.query_map(&[&id1,&j1], |row| {
//         Ok(Cat {
//             SampleId: row.get(0).expect("error get"),
//             SampleName: row.get(1).expect("error get"),
//             SampleNum: row.get(2).expect("error get"),
//             SampleWeight: row.get(3).expect("error get"),
//             Content01: row.get(6).expect("error get"),
//             Content02 : row.get(7).expect("error get"),

//         })
//     }).expect("error query");
//     for row in cats{
//         let o=row.unwrap();
//         println!("<id{},{}",o.SampleNum,o.SampleId);
//         return (o.SampleId,o.SampleNum.parse::<i32>().unwrap());
//     }
//     return (String::from(""),0);
// }
pub fn get_todo() -> Vec<(i32,String, bool)> {
    let mut vec=Vec::new();
    let conn = Connection::open("data.sqlite").expect("error open");
    let mut stmt = conn.prepare(
        "SELECT * from todos_todo",
    ).expect("error prepare");

    let todos=stmt.query_map([], |row| {
        Ok(Todo {
            id:row.get(0)?,
            text: row.get(1)?,
            completed: row.get(2)?,
        })
    }).expect("error query");
    for one in todos{
        match one {
            Ok(cat) => {
                vec.push((cat.id,cat.text,cat.completed));
            },
            Err(err) => {
                // println!("Failed to open the file.");
            }
        }
    }
    return vec;
}
pub fn main_do() -> Result<()> {

    let conn = Connection::open("data.sqlite")?;

    let mut todos = HashMap::new();
    todos.insert(String::from("Blue"), true);
    todos.insert(String::from("Black"), false);

    for (text, completed) in &todos {
        let  s_completed=if *completed {String::from("1")} else{String::from("0")};
        conn.execute(
            "INSERT INTO todos_todo (text,completed) values (?1,?2)",
            &[&text.to_string(),&s_completed]
        )?;
    }
    let mut stmt = conn.prepare(
        "SELECT * from todos_todo",
    )?;

    let todos = stmt.query_map([], |row| {
        Ok(Todo {
            id:row.get(0)?,
            text: row.get(1)?,
            completed: row.get(2)?,
        })
    })?;

    for cat in todos{
        println!("Found cat {:?}", cat);
    }

    Ok(())
}